package com.menu.web.controller.attribute;

import cn.hutool.core.util.RandomUtil;
import com.menu.common.annotation.Log;
import com.menu.common.core.controller.BaseController;
import com.menu.common.core.domain.AjaxResult;
import com.menu.common.enums.BusinessType;
import com.menu.common.enums.DataSourceType;
import com.menu.common.enums.Template;
import com.menu.common.utils.poi.ExcelUtil;
import com.menu.framework.datasource.DynamicDataSourceContextHolder;
import com.menu.system.domain.MenuPublicattributeType;
import com.menu.system.service.IMenuPublicattributeTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 产品共有属性类型Controller
 *
 * @author ruoyi
 * @date 2024-07-08
 */
@RestController
@RequestMapping("/system/type")
public class MenuPublicattributeTypeController extends BaseController {
    @Autowired
    private IMenuPublicattributeTypeService menuPublicattributeTypeService;

    /**
     * 查询产品共有属性类型列表
     */
    @PreAuthorize("@ss.hasPermi('system:type:list')")
    @GetMapping("/list")
    public AjaxResult list(MenuPublicattributeType menuPublicattributeType) {
        List<MenuPublicattributeType> list = menuPublicattributeTypeService.selectMenuPublicattributeTypeList(menuPublicattributeType);
        return success(list);
    }

    @PreAuthorize("@ss.hasPermi('system:type:list')")
    @GetMapping("/listTypete")
    public AjaxResult listTypete(MenuPublicattributeType menuPublicattributeType) {
        List<MenuPublicattributeType> list = menuPublicattributeTypeService.selectMenuPublicattributeTypeList(menuPublicattributeType);
        list.forEach(item -> {
            item.setPublicattributeName(item.getRemark());
        });
        return success(list);
    }

    /**
     * 导出产品共有属性类型列表
     */
    @PreAuthorize("@ss.hasPermi('system:type:export')")
    @Log(title = "产品共有属性类型", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MenuPublicattributeType menuPublicattributeType) {
        List<MenuPublicattributeType> list = menuPublicattributeTypeService.selectMenuPublicattributeTypeList(menuPublicattributeType);
        ExcelUtil<MenuPublicattributeType> util = new ExcelUtil<MenuPublicattributeType>(MenuPublicattributeType.class);
        util.exportExcel(response, list, "产品共有属性类型数据");
    }

    /**
     * 获取产品共有属性类型详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:type:query')")
    @GetMapping(value = "/{publicattributeId}")
    public AjaxResult getInfo(@PathVariable("publicattributeId") Long publicattributeId) {
        return success(menuPublicattributeTypeService.selectMenuPublicattributeTypeByPublicattributeId(publicattributeId));
    }

    /**
     * 新增产品共有属性类型
     */
    @PreAuthorize("@ss.hasPermi('system:type:add')")
    @Log(title = "产品共有属性类型", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MenuPublicattributeType menuPublicattributeType) {
        if (menuPublicattributeType.getPublicattributeName() == null || menuPublicattributeType.getPublicattributeName().equals("")) {
            return error(DynamicDataSourceContextHolder.getDataSourceType().equals(DataSourceType.MASTER.name()) ? "名称不能为空" : "The name cannot be empty");
        }

        if (menuPublicattributeType.getParentId().equals(0L)) {
            return toAjax(menuPublicattributeTypeService.insertMenuPublicattributeType(menuPublicattributeType));
        }

        menuPublicattributeType.setPublicattributeType(Template.PH + RandomUtil.randomStringWithoutStr(8, "0123456789"));
        return toAjax(menuPublicattributeTypeService.insertMenuPublicattributeType(menuPublicattributeType));


    }

    /**
     * 修改产品共有属性类型
     */
    @PreAuthorize("@ss.hasPermi('system:type:edit')")
    @Log(title = "产品共有属性类型", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MenuPublicattributeType menuPublicattributeType) {


        return toAjax(menuPublicattributeTypeService.updateMenuPublicattributeType(menuPublicattributeType));


    }

    /**
     * 删除产品共有属性类型
     */
    @PreAuthorize("@ss.hasPermi('system:type:remove')")
    @Log(title = "产品共有属性类型", businessType = BusinessType.DELETE)
    @DeleteMapping("/{publicattributeIds}")
    public AjaxResult remove(@PathVariable Long[] publicattributeIds) {

        return toAjax(menuPublicattributeTypeService.deleteMenuPublicattributeTypeByPublicattributeIds(publicattributeIds));
    }
}
